package io.zcoder.innoway.utils;


import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.v4.app.NotificationCompat;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

import io.zcoder.innoway.R;
import io.zcoder.innoway.activities.WelcomeActivity;
import io.zcoder.innoway.commons.Constants;

public class Utils {

    public static final String DATE_FORMAT_FULL = "yyyy-MM-dd'T'HH:mm:ss.SSS'Z'";
    public static final String DATE_FORMAT_YYYY_MM_DD_HH_MM_SS = "yyyy-MM-dd hh:mm:ss";
    public static final String DATE_FORMAT_YYY_MM_DD = "yyyy-MM-dd";
    public static final String DATE_FORMAT_MMM_DD_YYYY = "MMM dd, yyyy";
    public static final String DATE_FORMAT_MMM_DD_YYYY_HH_MM_AA = "MMM dd, yyyy - HH:mm aa";
    public static final String DATE_FORMAT_YYYY_MM = "yyyy-MM";
    public static final String DATE_FORMAT_MMMM_YYYY = "MMMM yyyy";

    public static boolean isBlank(String str) {
        int strLen;
        if (str == null || (strLen = str.length()) == 0) {
            return true;
        }
        for (int i = 0; i < strLen; i++) {
            if ((Character.isWhitespace(str.charAt(i)) == false)) {
                return false;
            }
        }
        return true;
    }

    public static int getOnlyFt(float cm) {
        int inch = (int) (cm / Constants.RATIO_INCH_TO_CM);
        return inch / Constants.RATIO_FT_TO_INCH;
    }

    public static int getOnlyInch(float cm) {
        int inch = (int) (cm / Constants.RATIO_INCH_TO_CM);
        int ft1 = inch / Constants.RATIO_FT_TO_INCH;
        int ft2 = inch % Constants.RATIO_FT_TO_INCH;
        return ft2;
    }

    public static String convertToInch(float cm) {
        int inch = (int) (cm / Constants.RATIO_INCH_TO_CM);
        int ft1 = inch / Constants.RATIO_FT_TO_INCH;
        int ft2 = inch % Constants.RATIO_FT_TO_INCH;
        return ft1 + "' " + ft2 + "\"";
    }

    public static void showNotification(Context context) {
        Intent myIntent = new Intent(context, WelcomeActivity.class);
        myIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_SINGLE_TOP);
        PendingIntent pendingIntent = PendingIntent.getActivity(context, 0, myIntent, PendingIntent.FLAG_UPDATE_CURRENT | PendingIntent.FLAG_ONE_SHOT);
        NotificationCompat.Builder mBuilder =
                new NotificationCompat.Builder(context)
                        .setSmallIcon(R.mipmap.ic_launcher)
                        .setContentTitle(context.getString(R.string.app_name))
                        .setContentIntent(pendingIntent)
                        .setAutoCancel(true)
                        .setContentText(context.getString(R.string.notification_text));
        Notification notification = mBuilder.build();
        notification.defaults |= Notification.DEFAULT_VIBRATE;
        notification.defaults |= Notification.DEFAULT_SOUND;
        NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
        notificationManager.notify(1, notification);
    }

    public static Date convertToDate(String source, String format) {
        if (source == null) return null;
        SimpleDateFormat formatter = new SimpleDateFormat(format);

        try {
            Date date = formatter.parse(source);
            return date;
        } catch (ParseException e) {
            e.printStackTrace();
        }
        return null;
    }

    public static String byteArraytoHex(byte[] bytes) {
        StringBuilder sb = new StringBuilder();
        for (byte b : bytes) {
            sb.append(String.format("%02X ", b));
        }
        return sb.toString();

    }

    public static String convertToString(Date source, String format) {
        if (source == null) return null;
        DateFormat df = new SimpleDateFormat(format, Locale.US);
        return df.format(source);
    }

    public static int getIdEntry(int position) {
        switch (position) {
            case 0:
                return R.string.weight;
            case 1:
                return R.string.bmi;
            case 2:
                return R.string.body_fat;
            case 3:
                return R.string.muscle;
            case 4:
                return R.string.water_s;
            case 5:
                return R.string.visceral_fat;
            case 6:
                return R.string.bone_mass;
            case 7:
                return R.string.bmr;
        }
        return 0;
    }

    public static String getUnitWeightHeight(int id) {
        switch (id) {
            case 0:
                return "Kg/Cm";
            case 1:
                return "Lbs/Ft";
        }
        return "Kg/Cm";
    }

    public static String getUnitWeight(int id) {
        switch (id) {
            case 0:
                return "kg";
            case 1:
                return "lbs";
            case 2:
                return "st";
            case 3:
                return "jin";
        }
        return "kg";
    }

    public static float getRatioWeight(int id) {
        switch (id) {
            case 0:
                return 1;
            case 1:
                return Constants.RATIO_KG_TO_LBS;
            case 2:
                return Constants.RATIO_KG_TO_ST;
            case 3:
                return Constants.RATIO_KG_TO_JIN;
        }
        return 1;
    }

    public static float formatFloat(float f) {
        String s = f + "";
        if (s.indexOf(".") <= s.length() - 2) {
            return Float.valueOf(s.substring(0, s.indexOf(".") + 2));
        }
        return f;
    }
}
